package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.News;
import com.example.springboot.service.NewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


@CrossOrigin
@RestController
@RequestMapping("/News")
public class NewsController {
    private static final Logger log = LoggerFactory.getLogger(NewsController.class);

    @Resource
    NewsService newsService;

    // 查询所有数据
    @GetMapping("/alldata")
    public Result getData() {
        return Result.success(newsService.GetAll());
    }

    /**
     * 新增新闻接口
     */
    @PostMapping
    public Result newsRegister(@RequestBody News news) {
        return Result.success(newsService.add(news));
    }

    /**
     * 根据ID删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        newsService.delete(id);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody News search,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        return Result.success(newsService.findPage(search, pageNum, pageSize));
    }

    /**
     * 编辑新闻接口
     */
    @PostMapping("/edit")
    public Result newsEdit(@RequestBody News news) {
        return Result.success(newsService.newsEdit(news));
    }




}
